'use strict';
const jwt = require('jsonwebtoken')
const Controller = require('egg').Controller;

class HomeController extends Controller {
  //请求数据
  async login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('userlist', { username, password })
    if (data) {
      ctx.body = {
        code: 200,
        msg: '登录成功',
        token: jwt.sign(data.id, 'dyh')
      }
    } else {
      ctx.body = {
        code: 404,
        msg: '用户名或密码错误'
      }
    }
  }
  async sgin(){
    const { ctx,app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('userlist', { username })
    if (data) {
      ctx.body = {
        code: 404,
        msg: '该账号已存在',
      }
    } else {
      await app.mysql.insert('userlist',{username,password,img:'',name:'游客',auth:'访客'})
      ctx.body = {
        code: 200,
        msg: '注册成功'
      }
    }
  }
}

module.exports = HomeController;